import {requestMenus} from "../api/api";
export const initMenu = (router, store)=> {
    requestMenus().then(res=>{
        let fmtRoutes = formatRoutes(res.data,0,[]);
        fmtRoutes.push({
            path: '/login',
            component: ()=>import('@/views/Login'),
            name: '',
            hidden: true
        })
        router.addRoutes(fmtRoutes);
        store.commit('initMenuStore', fmtRoutes);
        store.dispatch('connect');
    })
}
export const formatRoutes = (menus,parentId,routes)=> {
  menus.forEach(menu=>{
     if(menu.parentId == parentId){
       menu.component = ()=>import('@/views/'+menu.component)
       routes.push(menu)
       menu.children = []
       formatRoutes(menus,menu.id,menu.children)
     }
  })
  return routes
}
